package com.vinyl.font 
{
    import flash.display.Loader;
    import flash.events.*;
    import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.system.SecurityDomain;
	import flash.utils.getQualifiedClassName;
	
	import com.vinyl.events.DataEvent;
	
    public class FontLoader extends EventDispatcher
	{
		public var url:String;
        public var initFlag:Boolean = false;
		public var downloadProgresFlag:Boolean;
		
		public function FontLoader( url )
		{
			this.url = url;
			downloadProgresFlag = false;
        }
		
		public function load()
		{
			initFlag = true;
			var loader:Loader = new Loader();
			configureListeners(loader.contentLoaderInfo);
			
			var request:URLRequest = new URLRequest( url );
			var lc:LoaderContext = new LoaderContext;
			
			lc.applicationDomain = new ApplicationDomain( ApplicationDomain.currentDomain );
			if( Security.sandboxType == Security.REMOTE ) lc.securityDomain = SecurityDomain.currentDomain;
			loader.load( request, lc );
		}
		
        private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(Event.INIT, initHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
        }
		
        private function completeHandler(event:Event):void {
            //trace("completeHandler: " + event);
			dispatchEvent( new Event( Event.COMPLETE ) );
        }
		
        private function httpStatusHandler(event:HTTPStatusEvent):void {
            //trace("httpStatusHandler: " + event);
        }
		
        private function initHandler(event:Event):void {
            //trace("initHandler: " + event);
        }
		
        private function ioErrorHandler(event:IOErrorEvent):void {
            //trace("ioErrorHandler: " + event);
        }
		
        private function openHandler(event:Event):void {
            //trace("openHandler: " + event);
        }
		
        private function progressHandler(event:ProgressEvent):void {
            //trace("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
			downloadProgresFlag = true;
        }
    }
}